package com.hdoit.apollo.web.shop.controllers;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.mapper.BillingMapper;
import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.common.enums.shop.PayObject;
import com.hdoit.apollo.logical.shop.model.Payment;
import com.hdoit.apollo.logical.shop.service.IPaymentService;
import com.hdoit.apollo.logical.viewmodel.billing.BillingSearch;
import com.hdoit.apollo.logical.viewmodel.shop.GatheringStat;
import com.hdoit.apollo.logical.viewmodel.shop.PaymentSearch;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年06月01日 16:21:38
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/payForBilling/")
public class PayForBillingController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/payForBilling/";
    }

    @Autowired
    private IPaymentService paymentService;
    
    @Autowired
    private IBillingService billingService;
    
    @Autowired
    private BillingMapper billingMapper;

    private final String indexViewName="index";
    
    private final String otherViewName="other";
    
    private final String tabIndexViewName="tabIndex";
    private final String outputViewName="output";
    
    private final String otherOutputViewName="otherOutput";

    private final String deleteViewName = "delete";
    

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,BillingSearch search,boolean ajaxPost){
    	search.setType("notShipped");
    	GatheringStat stat=billingMapper.queryGatheringStat(search);
    	
        pagerModel.setTotalAmount(billingMapper.queryCountMPForPc(search));
        List<Billing> billings= billingMapper.queryListMPForPc(search, pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("billings", billings);
        model.addAttribute("billingSearch", search);
        model.addAttribute("stat", stat);
        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }
    
    @RequestMapping(value = otherViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String other(Model model,PagerModel pagerModel,PaymentSearch search,boolean ajaxPost){

    	search.setPayObject(PayObject.billing);
        pagerModel.setTotalAmount(paymentService.queryCountMP(search));
        List<Payment> payments= paymentService.queryListMP(search, pagerModel);
        System.err.println(search.getPayObject());
        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("payments", payments);
        model.addAttribute("paymentSearch", search);

        if(ajaxPost){
            return view(otherOutputViewName);
        }

        return view(otherViewName);
    }
    
    @RequestMapping(value = tabIndexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String tabIndex(Model model,PagerModel pagerModel,PaymentSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(paymentService.queryCountMP(search));
        List<Payment> payments= paymentService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("payments", payments);
        model.addAttribute("paymentSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(tabIndexViewName);
    }

    

   

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(paymentService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
}